<template>
	<view class="uni-navbar" :class="{'uni-navbar-fixed':isFixed,'uni-navbar-shadow':hasShadow}"
		:style="{'background-color':backgroundColor}">
		<uni-status-bar v-if="insertStatusBar"></uni-status-bar>
		<view class="uni-navbar-header" :style="{color:color}">
			<view class="uni-navbar-header-btns btnsLeft" @tap="onClickLeft">
				<view v-if="leftIcon.length" class="leftIcon" @tap="back()">
					<image src="/static/images/index/iconline.png" mode=""></image>
				</view <slot name="left">
				</slot>
			</view>
			<view class="uni-navbar-container">
				<view v-if="title.length" class="uni-navbar-container-title">{{title}}</view>
				<view v-else class="uni-navbar-input">

					<image src="/static/images/search.png" mode=""></image>
					<input type="text" placeholder="请输入应用关键字搜索" />
				</view>
				<!-- 标题插槽 -->
				<slot></slot>

			</view>
			<view class="uni-navbar-header-btns btnsFlex" @tap="onClickRight" style="width: 130rpx;">
				<view v-if="rightIcon.length" class="rightIcon">
					<image src="/static/images/nav/icon_gw_hover.png" mode=""></image>
				</view>
				<!-- 优先显示图标 -->
				<view v-if="rightText.length&&$store.state.token!=''" class="uni-navbar-btn-text rightIcon">
					{{rightText}}
				</view>
				<slot name="right"></slot>
			</view>
		</view>
	</view>
</template>

<script>
	import uniStatusBar from '../uni-status-bar/uni-status-bar.vue'
	import uniIcon from '../uni-icon/uni-icon.vue'

	export default {
		components: {
			uniStatusBar,
			uniIcon
		},
		props: {
			/**
			 * 标题文字
			 */
			title: {
				type: String,
				default: ''
			},
			urlType: {
				type: String,
				default: ''
			},
			/**
			 * 左侧按钮文本
			 */
			leftText: {
				type: String,
				default: ''
			},
			/**
			 * 右侧按钮文本
			 */
			rightText: {
				type: String,
				default: ''
			},
			/**
			 * 左侧按钮图标
			 */
			leftIcon: {
				type: String,
				default: ''
			},
			/**
			 * 右侧按钮图标
			 */
			rightIcon: {
				type: String,
				default: ''
			},
			/**
			 * 是否固定在顶部
			 */
			fixed: {
				type: [Boolean, String],
				default: false
			},
			/**
			 * 按钮图标和文字颜色
			 */
			color: {
				type: String,
				default: '#000000'
			},
			/**
			 * 背景颜色
			 */
			backgroundColor: {
				type: String,
				default: '#FFFFFF'
			},
			/**
			 * 是否包含状态栏，默认固定在顶部时包含
			 */
			statusBar: {
				type: [Boolean, String],
				default: ''
			},
			/**
			 * 是否使用阴影，默认根据背景色判断
			 */
			shadow: {
				type: String,
				default: ''
			}
		},
		computed: {
			back() {
				let canNavBack = getCurrentPages();
				if (this.urlType == 'order') {
					uni.redirectTo({
						url: '/pages/user/obligation?status=0' // 返回的页面数，如果 delta 大于现有页面数，则返回到首页。
					});
					return false
				}
				if (this.urlType == 'obligation') {
					uni.switchTab({
						url: '/pages/user/index' // 返回的页面数，如果 delta 大于现有页面数，则返回到首页。
					});
					return false
				}



				// 返回上一级页面
				if (canNavBack && canNavBack.length > 1) {
					uni.navigateBack();
				} else {
					history.back();
				}

			},
			isFixed() {
				return String(this.fixed) === 'true'
			},
			insertStatusBar() {
				switch (String(this.statusBar)) {
					case 'true':
						return true
					case 'false':
						return false
					default:
						return this.isFixed
				}
			},
			hasShadow() {
				var backgroundColor = this.backgroundColor
				switch (String(this.shadow)) {
					case 'true':
						return true
					case 'false':
						return false
					default:
						return backgroundColor !== 'transparent' && backgroundColor.indexOf('rgba') < 0
				}
			}
		},
		methods: {
			/**
			 * 左侧按钮点击事件
			 */
			onClickLeft() {
				this.$emit('clickLeft')
				this.$emit('click-left')
			},
			/**
			 * 右侧按钮点击事件
			 */
			onClickRight() {
				this.$emit('custom-event')
				this.$emit('click-right')
			}
		}
	}
</script>

<style scoped lang="scss">
	.leftIcon {
		width: 80%;
		display: flex;
		align-items: center;
		justify-content: flex-start;
		cursor: pointer;

		image {
			width: 40rpx;
			height: 40rpx;
			margin-left: 10rpx;
		}
	}

	.rightIcon {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		position: absolute;
		right: 0rpx;
		color: #6954D5;
		font-size: 26rpx;
		image {
			width: 40rpx;
			height: 40rpx;
		}
	}

	.uni-navbar {
		display: block;
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		background-color: #FFFFFF;
		overflow: hidden;
		z-index: 9;
		
	}

	.uni-navbar view {
		line-height: 44px;
	}

	.uni-navbar-shadow {
		/* box-shadow: 0 1px 6px #ccc; */
	}

	.uni-navbar.uni-navbar-fixed {
		position: fixed;
		z-index: 998;
	}

	.uni-navbar-header {
		display: flex;
		flex-direction: row;
		width: 100%;
		height: 44px;
		line-height: 44px;
		font-size: 16px;
	}

	.uni-navbar-header .uni-navbar-header-btns {
		display: inline-flex;
		flex-wrap: nowrap;
		flex-shrink: 0;
		width: 70rpx;
		align-items: center;
		justify-content: space-around
	}

	.uni-navbar-container {
		width: 100%;
		margin: 0 10rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.uni-navbar-container-title {
		font-size: 30rpx;
		text-align: center;
	}

	.uni-navbar-input {
		width: 80%;
		border-radius: 19px;
		border: 4rpx solid #6954D5;
		height: 74rpx;
		position: relative;
		padding: 0 40rpx;
		padding-left: 90rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: flex-start;

		image {
			width: 40rpx;
			height: 40rpx;
			position: absolute;
			left: 20rpx;
		}

		input {
			font-size: 24rpx;
			height: 74rpx;
			line-height: 50rpx;
		}
	}

	.btnsFlex {
		width: 130rpx;
		height: 100%;
		position: absolute;
		right: 0;
	}

	.btnsLeft {
		width: 130rpx;
		height: 100%;
		position: absolute;
		left: 0;
	}
</style>